#include <stdio.h>


#define SIMD_FORCE_INLINE __forceinline
#define ATTRIBUTE_ALIGNED16(a) __declspec(align(16)) a
#define ATTRIBUTE_ALIGNED64(a) __declspec(align(64)) a
#define ATTRIBUTE_ALIGNED128(a) __declspec (align(128)) a

int GetSum(const int *pArray,const int ArraySize)
{
	int Sum = 0;

	for(int i=0;i<ArraySize;i++)
	{
		Sum+= pArray[i];
	}

	return Sum;
}

int GetSumBySIMD(int *pArray,const int ArraySize)
{
	__declspec(align(16)) int Sum[4] ={0};
	

	for(int i=0;i<ArraySize;i=i+4)
	{
		int* pStackPointer = &pArray[i];
		__asm
		{
			pushad

			mov		eax,pStackPointer
	
			movdqu	xmm0,Sum
			movdqu	xmm1,[eax]
		
			paddd	xmm1,xmm0
			movdqu	Sum,xmm1

			popad
			emms
		}
	}

	return Sum[0]+Sum[1]+Sum[2]+Sum[3];
}

void main(void)
{
	const int ArraySize = 1000;
	__declspec(align(16))	int Array[ArraySize];

	for(int i=0;i<ArraySize;i++)
	{
		Array[i] = i;
	}

	printf("%d \n",GetSum(Array,ArraySize));
	printf("%d \n",GetSumBySIMD(Array,ArraySize));

}